<div v-cloak id="app" class="main-content">
    <div class="form-body">
        <one-form :config="config" v-model="fdata" ref="ruleForm">
            <div slot="menuLeft">
                <el-button
                    class="button-item"
                    :loading="btnLoading"
                    type="primary"
                    @click="store('ruleForm')"
                    size="small"
                >
                    保存
                </el-button>
                <el-button size="small" @click="backpage()">返回</el-button>
            </div>
            <!-- 上传图片 -->
            <template slot="image" slot-scope="scope">
                <one-upload
                    class="one-attachment-simple-upload"
                    v-loading="uploading"
                    :disabled="uploading"
                    :accept="'image'"
                    @success="uploadSuccess($event,scope.row.prop)"
                    flex="main:left cross:center"
                >
                    <el-avatar
                        fit="scale-down"
                        shape="square"
                        size="60"
                        :src="fdata[scope.row.prop]"
                    ></el-avatar>
                </one-upload>
            </template>
        </one-form>
    </div>
</div>
{include file="common@components/one-form"} {include
file="common@components/one-upload"}
<script>
    const _formData = {:json_encode((array)$formData, 1)} ;
    const _level_select = {:json_encode((array)$_level_select, 1)} ;
    const app = new Vue({
        el: "#app",
        data() {
            return {
                uploading:false,
                loading:false,
                btnLoading:false,
                config: {
                    formdesc: [
                        {
                            label: "用户名 :",
                            prop: "username",
                            type: "input",
                            rules:[{ required: true, message: "用户名不能为空"}],
                            bind: {
                                "placeholder": "请输入用户名",
                            },
                        },
                        {
                            label: "昵称 :",
                            prop: "nickname",
                            type: "input",
                            rules:[{ required: true, message: "昵称不能为空"}],
                            bind: {
                                "placeholder": "请输入昵称",
                            },
                        },
                        {
                            label: "密码 :",
                            prop: "password",
                            type: "input",
                            rules:[{ required: true, message: "密码不能为空"}],
                            bind: {
                                'show-password':true,
                                "placeholder": "请输入密码",
                            },
                        },
                        {
                            label: "会员等级 :",
                            prop: "level_id",
                            type: "select",
                            options:_level_select || [],
                            rules:[{ required: true, message: "会员等级不能为空"}],
                            bind: {
                                "placeholder": "请输入会员等级",
                            },
                        },
                        {
                            label: "手机号 :",
                            prop: "mobile",
                            type: "input",
                            rules:[{ required: false, message: "手机号不能为空"}],
                            bind: {
                                "placeholder": "请输入手机号",
                            },
                        },
                    ],
                    labelWidth:"200px",
                    rowSize: 1, //一行可以展示几列表单，默认为3列
                },
                fdata:{
                    id: 0,
                    title: "",
                    img: "",
                },
            }
        },
        created() {
        },
        mounted(){
            if (Object.keys(_formData).length > 0) {
                for(let key in this.fdata){
                    this.fdata[key] = _formData[key];
                }
                this.config.formdesc.splice(2,1)
            }
        },
        methods: {
            //表单验证
            getFormPromise(form) {
                return new Promise(resolve => {
                    form.validate(res => {
                        resolve(res);
                    })
                })
            },
            store(formName) {
                let _this = this;
                // 获取到组件中的form
                const configForm = this.$refs.ruleForm.$refs.ruleForm;
                // 使用Promise.all去校验结果,可加入多个表单
                Promise.all([configForm].map(this.getFormPromise)).then(res => {
                    const validateResult = res.every(item => !!item);
                    if (validateResult) {
                        _this.loading = true;
                        _level_select.some((v,i,a)=>{
                            if(v.value  == _this.fdata.level_id){
                                _this.fdata.level_name = v.label;
                            }
                        })
                        request({
                            params: {
                                s: "member/index/edit"
                            },
                            method: "post",
                            data: _this.fdata
                        }).then(e => {
                            _this.loading = false;
                            if (e.data.code == 0) {
                                _this.backpage()
                            } else {
                                _this.$message.error(e.data.msg);
                            }
                        })

                    } else {
                        console.log("表单未校验通过");
                    }
                })

            },
            backpage(){
                navigateTo({
                    s: "member/index/index",
                })
            },
            //图片上传完成
            uploadSuccess(file,prop){
                file=file.response.data.data.file
                this.fdata[prop] = file
            },
        },

    });
</script>
